/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.ejbs;

import cassys.application.services.ModuleManagementBeanRemote;
import cassys.data.DAOFactory;
import cassys.data.LectureDAO;
import cassys.data.ModuleDAO;
import cassys.domain.classManagement.Lecture;
import cassys.domain.classManagement.Module;
import cassys.domain.classManagement.ModuleType;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;

/**
 *
 * @author User
 */
@Stateless(mappedName = "ModuleManagementBean")
//@TransactionManagement(TransactionManagementType.CONTAINER)
public class ModuleManagementBean implements ModuleManagementBeanRemote {

    //@Resource
    // private SessionContext context;
    DAOFactory daoFactory = DAOFactory.instance(DAOFactory.HIBERNATE);
    ModuleDAO moduleRepository;
    LectureDAO lectureRepository;

    @PostConstruct
    public void init() {
        System.out.println("Post Construct : Module Manager Bean");
        moduleRepository = daoFactory.getModuleDAO();
        lectureRepository = daoFactory.getLectureDAO();
    }

    @Override
    public List<Lecture> getAllLecturers() {
        System.out.println("in module management bean");
        lectureRepository = daoFactory.getLectureDAO();
        List<Lecture> lectures = lectureRepository.findAll();
        return lectures;
    }

    @Override
    public List<ModuleType> getModuleTypes() {
        return Arrays.asList(ModuleType.values());
    }

    @Override
    //@TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void createModule(Module module) {
        System.out.println("creating a new module");


        try {
            Lecture owner = lectureRepository.findById(module.getOwner().getLectureId(), false);
            Module newModule = new Module(module.getModuleCode(), module.getTitle(), module.getType(), owner);
            for (Lecture lecturer : module.getLectures()) {
                newModule.addLecture(lectureRepository.findById(lecturer.getLectureId(), false));
            }
            System.out.println("Make Persistent");
            System.out.println("lecturere count: " + newModule.getLectures().size());
            System.out.println("Persisting info :" + newModule.toString());
            moduleRepository.makePersistent(newModule);
            System.out.println("Persistent done");

        } catch (Exception e) {
            System.out.println("err:" + e.getMessage());
            System.out.println("err:" + e.getStackTrace());
        }
    }

//    private Module mapNewModule(Module module) {
//        Lecture owner = lectureRepository.findById(module.getOwner(), false);
//
//        Module newModule = new Module(module.getModuleCode(), module.getModuleTitle(), ModuleType.core, owner);
//
//        long[] lectures = module.getLectures();
//        for (int i = 0; i < lectures.length; i++) {
//            newModule.addLecture(lectureRepository.findById(lectures[i], false));
//        }
//        return newModule;
//    }
    @Override
    public List<Module> getAllModules() {
        return moduleRepository.findAll();
    }

    @Override
    public void updateModule(Module module) {
        System.out.println("updateing a new module");


        try {
            System.out.println(">>>>>" + module.getOwner().getLectureId());
            Module existingModule = moduleRepository.findById(module.getModuleId(), false);
            // existingModule.getLectures().clear();
            System.out.println("===>>>" + existingModule.getLectures().size());

            Lecture newOwner = lectureRepository.findById(module.getOwner().getLectureId(), false);
            System.out.println(" new owner retrieved from db, the name is : " + newOwner.getName());
            Lecture currentOwner = lectureRepository.findById(existingModule.getOwner().getLectureId(), false);
            System.out.println(" current owner retrieved from db, the name is :) " + currentOwner.getName());
            System.out.println(" cleaning up the lectures... ");
            existingModule.getLectures().clear();
            for (Lecture lecturer : module.getLectures()) {
                System.out.println(" adding lecture to existing module... ");
                existingModule.getLectures().add(lectureRepository.findById(lecturer.getLectureId(), false));
            }
            System.out.println(" comparing the owners... ");

            if (!currentOwner.equals(newOwner)) {
                System.out.println(" Different Owners, so swap!!! ");
                existingModule.swapOwner(newOwner);
            }

            System.out.println("===>>>" + existingModule.getLectures().size());
            System.out.println("Make Persistent");
            moduleRepository.makePersistent(existingModule);
            System.out.println("Persistent done");

        } catch (Exception e) {
            System.out.println("err:" + e.getMessage());
            System.out.println("err:" + e.getStackTrace());
        }
    }

    @Override
    public void deleteModule(Module module) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Module findByModuleId(long moduleId) {
        return this.moduleRepository.findById(moduleId, false);
    }
//    @Override
//    public List<ModuleDTO> getAllModules() {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
}
